/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaz;

import datos.FacturaDB;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import negocio.Factura;

/**
 *
 * @author F35
 */
public class FacturasLista extends javax.swing.JInternalFrame {

    /**
     * Creates new form FacturasLista
     */
    FacturaDB facturaBD = new FacturaDB();
    menu menu;

    public FacturasLista(menu m) {
        initComponents();
        actualizar();
        this.menu = m;
    }

    private FacturasLista() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtBusqueda = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        Tfacturas = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(1000, 576));

        jLabel1.setText("Buscar");

        txtBusqueda.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtBusquedaCaretUpdate(evt);
            }
        });

        Tfacturas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        Tfacturas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TfacturasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(Tfacturas);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 974, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtBusqueda)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 443, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(62, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtBusquedaCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtBusquedaCaretUpdate
        // TODO add your handling code here:
    }//GEN-LAST:event_txtBusquedaCaretUpdate

    private void TfacturasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TfacturasMouseClicked

        if (evt.getClickCount() == 1 && Tfacturas.getSelectedRow()!=-1) {
            try {
                ArrayList<String> clave = new ArrayList();

                clave.add(Tfacturas.getValueAt(Tfacturas.getSelectedRow(), 0).toString());
                clave.add(Tfacturas.getValueAt(Tfacturas.getSelectedRow(), 1).toString());
                clave.add(Tfacturas.getValueAt(Tfacturas.getSelectedRow(), 2).toString());
                MostrarFactura mf = new MostrarFactura(clave, menu,this);
                this.setVisible(false);
                menu.escritorio.add(mf);
                mf.show();
            } // TODO add your handling code here:
            catch (SQLException ex) {
                Logger.getLogger(FacturasLista.class.getName()).log(Level.SEVERE, null, ex);
            }





        }








        // TODO add your handling code here:
    }//GEN-LAST:event_TfacturasMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FacturasLista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FacturasLista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FacturasLista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FacturasLista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FacturasLista().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable Tfacturas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtBusqueda;
    // End of variables declaration//GEN-END:variables

    private void actualizar() {
        try {
            ArrayList<Factura> facturas = facturaBD.listar();
            String[] columnas = {"IdFactura", "Fecha", "Cliente"};
            String[][] datos = new String[facturas.size()][3];
            for (int i = 0; i < facturas.size(); i++) {

                Factura f = facturas.get(i);
                datos[i][0] = f.getIdFactura();
                datos[i][1] = f.getFecha();
                datos[i][2] = f.getCedula();

            }
            DefaultTableModel aux = new DefaultTableModel(datos, columnas);
            Tfacturas.setModel(aux);
            //Tfacturas.setEnabled(false);
            
            Tfacturas.updateUI();

        } catch (SQLException ex) {
            Logger.getLogger(FacturasLista.class.getName()).log(Level.SEVERE, null, ex);
        }


    }
}
